{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 类与对象\n",
    "\n",
    "对象 = 属性 + 方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Turtle:\n",
    "    head = 1\n",
    "    eyes = 2\n",
    "    legs = 4\n",
    "    shell = True\n",
    "    \n",
    "    def crawl(self):\n",
    "        print('我正在爬，我正在爬...')\n",
    "    \n",
    "    def run(self):\n",
    "        print('在跑了在跑了...')\n",
    "    \n",
    "    def bite(self):\n",
    "        print('别动我！')\n",
    "\n",
    "    def eat(self):\n",
    "        print('饿啦饿啦！吃饭吃饭！')\n",
    "\n",
    "    def sleep(self):\n",
    "        print('该睡觉了~Zzzzzz')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1 = Turtle()\n",
    "t2 = Turtle()\n",
    "t1.head"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1.legs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该睡觉了~Zzzzzz\n"
     ]
    }
   ],
   "source": [
    "t1.sleep()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "在跑了在跑了...\n"
     ]
    }
   ],
   "source": [
    "t1.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "t1.head=3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 1)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1.head, t2.head"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "t1.mouth = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1.mouth"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['__class__',\n",
       " '__delattr__',\n",
       " '__dict__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__getattribute__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__le__',\n",
       " '__lt__',\n",
       " '__module__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__setattr__',\n",
       " '__sizeof__',\n",
       " '__str__',\n",
       " '__subclasshook__',\n",
       " '__weakref__',\n",
       " 'bite',\n",
       " 'crawl',\n",
       " 'eat',\n",
       " 'eyes',\n",
       " 'head',\n",
       " 'legs',\n",
       " 'mouth',\n",
       " 'run',\n",
       " 'shell',\n",
       " 'sleep']"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(t1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['__class__',\n",
       " '__delattr__',\n",
       " '__dict__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__getattribute__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__le__',\n",
       " '__lt__',\n",
       " '__module__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__setattr__',\n",
       " '__sizeof__',\n",
       " '__str__',\n",
       " '__subclasshook__',\n",
       " '__weakref__',\n",
       " 'bite',\n",
       " 'crawl',\n",
       " 'eat',\n",
       " 'eyes',\n",
       " 'head',\n",
       " 'legs',\n",
       " 'run',\n",
       " 'shell',\n",
       " 'sleep']"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dir(t2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 创建出来类以后，属性值可以任意改变\n",
    "- 不同的实例对象，不共享数据\n",
    "- 可以添加属性\n",
    "- 类的第一个参数一定是self"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 继承性\n",
    "\n",
    "可以同时继承多个类\n",
    "\n",
    "若含有相同的属性，优先级是从左到右"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "class A:\n",
    "    x = 520\n",
    "    y = 5\n",
    "    def hello(self):\n",
    "        print('我是类A')\n",
    "\n",
    "class B(A): #继承A类\n",
    "    x = 666\n",
    "    def hello(self):\n",
    "        print(\"我是B\")\n",
    "    pass\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 子类可以访问到父类的属性与方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = B()\n",
    "b.y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- 若子类和父类拥有相同的属性和方法，优先访问子类"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "666"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我是B\n"
     ]
    }
   ],
   "source": [
    "b.hello()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 组合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Dog:\n",
    "    def say(self):\n",
    "        print('汪汪汪...')\n",
    "\n",
    "class Cat:\n",
    "    def say(self):\n",
    "        print(\"喵喵喵...\")\n",
    "class Bird:\n",
    "    def say(self):\n",
    "        print('叽叽喳喳...')\n",
    "\n",
    "class Garden:\n",
    "    d = Dog()\n",
    "    c = Cat()\n",
    "    b = Bird()\n",
    "    def say(self):\n",
    "        self.d.say() #实例对象 和方法 进行绑定\n",
    "        self.c.say() # 实例对象可以有千万个，这里调用的是 Garden类自己中的实例对象 因此要加 self\n",
    "        self.b.say()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "汪汪汪...\n",
      "喵喵喵...\n",
      "叽叽喳喳...\n"
     ]
    }
   ],
   "source": [
    "g = Garden()\n",
    "g.say()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 查询实例对象的属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'head': 3, 'mouth': 1}"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t1.__dict__ #可以查询实例化对象的 属性值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 通过方法对实例对象进行赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "class A():\n",
    "    def set_x(self,var):\n",
    "        self.x = var"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "a1 = A()\n",
    "a2 = A()\n",
    "a1.set_x(520)\n",
    "a2.set_x('你好')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(520, '你好')"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1.x,a2.x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 实例化对象的同时进行个性化定制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "class C:\n",
    "    def __init__(self,x,y): #在实例化的时候，就会被执行\n",
    "        self.x = x\n",
    "        self.y = y\n",
    "    def add(self):\n",
    "        return self.x + self.y\n",
    "    def mul(self):\n",
    "        return self.x * self.y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = C(2,3)\n",
    "c.add()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.13 ('pytorch')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "1762c40d460eb7b1689027fea8a1da294edc5d62cc54335a55926ac4d269017f"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
